import Vue from 'vue';

// let domain = 'http://admin.jlp.iydsj.com/bg';//线上服务器
// let domain_node = "http://admin.jlp.iydsj.com/nd";//线上

// let domain = 'http://test.admin.jlp.iydsj.com/bg';//131-服务器
let domain_node = "http://test.admin.jlp.iydsj.com/nd";//131-node服务器

let domain = localStorage.getItem("domain") || '/api_185';

export default {
	domain,
	domain_node,
	confirmBtnText: {
		confirmButtonText: "确定",
		cancelButtonText: "取消"
	},

	getUrlParam(name) {
		let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		let url = window.location.href;

		let index = url.indexOf('?');
		let query = url.substr(index);

		let r = query.substr(1).match(reg);  //匹配目标参数
		if (r != null) return decodeURI(r[2]); return null; //返回参数值
	},
	toBase64(data) {
		let toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
		let base64Pad = '=';
		let result = '';
		let length = data.length;
		let i;

		// Convert every three bytes to 4 ascii characters.                                                 

		for (i = 0; i < (length - 2); i += 3) {
			result += toBase64Table[data.charCodeAt(i) >> 2];
			result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
			result += toBase64Table[((data.charCodeAt(i + 1) & 0x0f) << 2) + (data.charCodeAt(i + 2) >> 6)];
			result += toBase64Table[data.charCodeAt(i + 2) & 0x3f];
		}

		// Convert the remaining 1 or 2 bytes, pad out to 4 characters.                                     
		if (length % 3) {
			i = length - (length % 3);
			result += toBase64Table[data.charCodeAt(i) >> 2];
			if ((length % 3) == 2) {
				result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
				result += toBase64Table[(data.charCodeAt(i + 1) & 0x0f) << 2];
				result += base64Pad;
			} else {
				result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];
				result += base64Pad + base64Pad;
			}
		}

		return result;
	},
	//数组排序
	sortby(name) {
		return function (o, p) {
			let a, b;
			if (typeof o === "object" && typeof p === "object" && o && p) {
				a = o[name];
				b = p[name];
				if (a === b) {
					return 0;
				}
				if (typeof a === typeof b) {
					return a < b ? -1 : 1;
				}
				return typeof a < typeof b ? -1 : 1;
			} else {
				throw "error";
			}
		};
	},
	successMsg(msg, title = "成功") {
		Vue.prototype.$notify({
			type: "success",
			title: title,
			message: msg,
			duration: 3000,
			offset: 80
		})
	},
	errorMsg(msg, title = "错误", type = "error") {
		Vue.prototype.$notify({
			type: type,
			title: title,
			message: msg,
			duration: 3000,
			offset: 80
		})
	},
	//页面跳转；
	refreshTo(url, params) {
		if (params) {
			for (let k in params) {
				url += `&${k}=${params[k]}`;
			}
			url = url.replace("&", "?");
		}
		location.href = url;
	},
	// 存储localStorage
	setStore(name, content) {
		if (!name) return;
		if (typeof content !== 'string') {
			content = JSON.stringify(content);
		}
		window.localStorage.setItem(name, content);
	},
	// 获取localStorage
	getStore(name) {
		if (!name) return;
		return window.localStorage.getItem(name);
	},
	//删除localStorage
	removeStore(name) {
		if (!name) return;
		if (name instanceof Array) {
			name.forEach(item => {
				window.localStorage.removeItem(item);
			});
			return;
		}
		window.localStorage.removeItem(name);
	},
	//获取省市
	getCities() {
		return new Promise((resolve, reject) => {
			Vue.prototype.$axios.get("/adminapi/v1/campus/area/childes/list?parentId=00000000").then(response => {
				if (response.data.error == 10000) {
					resolve(response.data.data);
				} else {
					reject();
				}
			})
		});

	},
	//根据地区id返回省市，区县
	getCityAndZoneById(id) {
		return new Promise(resolve => {
			let city, cityId = id.substring(0, 2) + "000000", zoneId = id.substring(0, 4) + "0000";
			this.getCities().then(cities => {
				cities.forEach(item => {
					if (item.areaId == cityId) city = item;
				})

				this.getZones(cityId).then(zones => {
					let zone;
					zones.forEach(option => {
						if (option.areaId == zoneId) zone = option;
					})
					resolve({ city, zone });
				})
			})
		})
	},
	//获取区县
	getZones(cityId) {
		return new Promise((resolve, reject) => {
			Vue.prototype.$axios.get("/adminapi/v1/campus/area/childes/list?parentId=" + cityId).then(response => {
				if (response.data.error == 10000) {
					resolve(response.data.data);
				} else {
					reject();
				}
			})
		});
	},
	//获取学校列表
	getSchools(areaId = '00000000') {
		return new Promise((resolve, reject) => {
			let params = { areaId: areaId };
			Vue.prototype.$axios.get("/adminapi/v1/tools/schools/search", {
				params: params
			}).then(response => {
				if (response.data.error == 10000) {
					resolve(response.data.data || []);
				} else {
					reject();
				}
			})
		});
	},
	//获取班级列表
	getClasses(schoolId) {
		return new Promise((resolve, reject) => {
			Vue.prototype.$axios.post("/adminapi/v1/tools/classes", { schoolId }).then(response => {
				if (response.data.error == 10000) {
					resolve(response.data.data);
				} else {
					reject();
				}
			})
		});
	},
	//获取教师列表
	getTeachers(schoolId) {
		return new Promise((resolve, reject) => {
			Vue.prototype.$axios.post("/adminapi/v1/tools/teachers", { schoolId }).then(response => {
				if (response.data.error == 10000) {
					resolve(response.data.data);
				} else {
					reject();
				}
			})
		});
	},
	//根据学校类型获取年级; schoolType:[];不传参数，返回所有年级
	getGrades(schoolType = ["小学", "初中", "高中"]) {
		let grades = [];

		if (schoolType.includes("小学")) {
			grades = [
				{ gradeName: "一年级", gradeId: 1 },
				{ gradeName: "二年级", gradeId: 2 },
				{ gradeName: "三年级", gradeId: 3 },
				{ gradeName: "四年级", gradeId: 4 },
				{ gradeName: "五年级", gradeId: 5 },
				{ gradeName: "六年级", gradeId: 6 }
			];
		}

		if (schoolType.includes("初中")) {
			grades = grades.concat([
				{ gradeName: "初一", gradeId: 7 },
				{ gradeName: "初二", gradeId: 8 },
				{ gradeName: "初三", gradeId: 9 }
			]);
		}

		if (schoolType.includes("高中")) {
			grades = grades.concat([
				{ gradeName: "高一", gradeId: 10 },
				{ gradeName: "高二", gradeId: 11 },
				{ gradeName: "高三", gradeId: 12 }
			]);
		}

		return grades;
	},
	gradeFormat(grade) {
		switch (grade) {
			case 1:
				grade = '一年级'
				break;
			case 2:
				grade = '二年级'
				break;
			case 3:
				grade = '三年级'
				break;
			case 4:
				grade = '四年级'
				break;
			case 5:
				grade = '五年级'
				break;
			case 6:
				grade = '六年级'
				break;
			case 7:
				grade = '初一'
				break;
			case 8:
				grade = '初二'
				break;
			case 9:
				grade = '初三'
				break;
			case 10:
				grade = '高一'
				break;
			case 11:
				grade = '高二'
				break;
			case 12:
				grade = '高三'
				break;
		}
		return grade;
	}
}

Date.prototype.format = function (format) {
	var date = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth() + 3) / 3),
		"S+": this.getMilliseconds()
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	for (var k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1
				? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
		}
	}
	return format;
}
// 优化toFixed()方法的精度
if (!Number.prototype._toFixed) {
	Number.prototype._toFixed = Number.prototype.toFixed;
}
Number.prototype.toFixed = function (n) {
	return (this + 1e-14)._toFixed(n);
};